package com.scoressystem.controller;

import com.scoressystem.pojo.Scores;
import com.scoressystem.pojo.Subjects;
import com.scoressystem.service.ScoresService;
import com.scoressystem.service.SubjectsService;
import com.scoressystem.utils.Results;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/score")
public class ScoresController {

    @Resource
    ScoresService ScoreService;

    @Resource
    SubjectsService SubjectsService;

    @RequestMapping("/getAllScore")
    public Results<List<Scores>> ScorePage(@RequestParam(value = "stuName", defaultValue = "") String stuName,
                                           @RequestParam(value = "page", defaultValue = "1") int pageIndex,
                                           @RequestParam(value = "limit", defaultValue = "5") int pageSize) {

        Map<String, Object> map = new HashMap<>();
        map.put("stuName", stuName);
        map.put("pageStart", (pageIndex - 1) * pageSize);
        map.put("pageSize", pageSize);

        List<Scores> Scores = ScoreService.selectAllScoresByPage(map);
        long total = ScoreService.selectAllScoresCount(map);
        return new Results<>("0", "查询成功", Scores, total);

    }

    @PostMapping("/updateScore")
    public Results<String> updateScore(Scores Score) {
        int result = ScoreService.updateScore(Score);
        if (result > 0) {
            return new Results<>("0", "更新成功");
        } else {
            return new Results<>("1", "更新失败");
        }
    }

    @RequestMapping("/deleteScore")
    public Results<String> deleteScore(@RequestParam("id") Integer ScoreNo) {
        int result = ScoreService.deleteScoreById(ScoreNo);
        if (result > 0) {
            return new Results<>("0", "删除成功");
        } else {
            return new Results<>("1", "删除失败");
        }
    }

    @RequestMapping("/addScore")
    public Results<String> addScore(Scores score) {
//        System.out.println(score.getStuId()+", " + score.getSubName());
//
//        Subjects subjectsByName = SubjectsService.getSubjectsByName(score.getSubName());
//        System.out.println("科目查询结果: " + subjectsByName);
//        System.out.println("学生ID: " + score.getStuId() + ", 科目ID: " + subjectsByName.getId());
        System.out.println(score.getSubName());
        // 检查：同一学生（stuId）的同一科目（subId）是否已存在成绩
        Scores existingScore = ScoreService.getScoreByStuIdAndSubId(score.getStuId(), score.getSubId());
        if (existingScore != null) {
            return new Results<>("4", "该学生的此科目成绩已存在");
        }
        // 新增成绩（假设id是自增，无需手动设置）
        int result = ScoreService.addScore(score);
        if (result > 0) {
            return new Results<>("0", "添加成功");
        } else {
            return new Results<>("1", "添加失败");
        }
    }

    @RequestMapping("/getMyScore")
    public Results<List<Scores>> getMyScore(HttpSession session) {
        Integer stuId = (Integer) session.getAttribute("studentNo");
        List<Scores> scores = ScoreService.getMyAllSubjectsByStuId(stuId);
        if (scores != null && !scores.isEmpty()) {
            return new Results<>("0", "查询成功", scores);
        } else {
            return new Results<>("1", "没有找到相关成绩");
        }
    }

    @RequestMapping("/getAllSubjectsAndMyScores")
    public Results<Map<String, Object>> getAllSubjectsAndMyScores(HttpSession session) {
        Integer stuId = (Integer) session.getAttribute("studentNo");
        List<Subjects> subjectsList = SubjectsService.getAllSubjects();
        List<Scores> scoresList = ScoreService.getMyAllSubjectsByStuId(stuId);
        List<Scores> allScoresList = ScoreService.getAllScoresBySname("");

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("subjects", subjectsList);
        resultMap.put("myScores", scoresList);
        resultMap.put("allScores", allScoresList);

        return new Results<>("0", "查询成功", resultMap);
    }
}
